<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="第几周" prop="week">
        <el-input
          v-model="queryParams.week"
          placeholder="请输入第几周"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="星期几" prop="day">
        <el-input
          v-model="queryParams.day"
          placeholder="请输入星期几"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="第几节" prop="section">
        <el-input
          v-model="queryParams.section"
          placeholder="请输入第几节课"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教师名称" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入教师名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="教室名称" prop="teacherName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入教室名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="课程名称" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年份" prop="year">
        <el-input
          v-model="queryParams.year"
          placeholder="请输入年份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学期" prop="semester">
        <el-input
          v-model="queryParams.semester"
          placeholder="请输入学期(1-上半学期，2-下半学期)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['edu:sch:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['edu:sch:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['edu:sch:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['edu:sch:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="schList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="第几周" align="center" prop="week" />
      <el-table-column label="星期几" align="center" prop="day" />
      <el-table-column label="第几节课" align="center" prop="section" />
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="教师名称" align="center" prop="teacherName" />
      <el-table-column label="班级" align="center" prop="className" />
      <el-table-column label="教室" align="center" prop="roomName" />
      <el-table-column label="年份" align="center" prop="year" />

      <el-table-column
        label="学期(1-上半学期，2-下半学期)"
        align="center"
        prop="semester"
      />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['edu:sch:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['edu:sch:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="第几周" prop="week">
          <!-- <el-input v-model="form.year" placeholder="请输入年级" /> -->
          <el-select v-model="form.week" placeholder="请选择第几周"  :disabled="starweek!=null ||endweek !=null ">
            <el-option
              v-for="item in 16"
              :key="item.id"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <span style="color: red;">单次只能选择单次插入或者批量插入</span>
        </el-form-item>
        <el-form-item label="批量选择周数">
          <el-col :span="11">
      <el-select v-model="starweek" placeholder="开始周数"  :disabled="form.week!=null">
            <el-option
              v-for="item in 16"
              :key="item.id"
              :label="item"
              :value="item"
            ></el-option>
        </el-select>
    </el-col>
   
    <el-col class="line" :span="2" style="text-align: center;">-</el-col>
    <el-col :span="11">
      <el-select v-model="endweek" placeholder="结束周数"  :disabled="form.week!=null">
            <el-option
              v-for="item in 16"
              :key="item.id"
              :label="item"
              :value="item"
            ></el-option>
        </el-select>
    </el-col>
  </el-form-item>





        <el-form-item label="星期几" prop="day">
          <!-- <el-input v-model="form.year" placeholder="请输入年级" /> -->
          <el-select v-model="form.day" placeholder="请输入星期几">
            <el-option
              v-for="item in 12"
              :key="item.id"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="第几节" prop="section">
          <el-select v-model="form.section" placeholder="请输入第几节"  :disabled="starsection!=null || endsection !=null ">
            <el-option
              v-for="item in 13"
              :key="item.id"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>



        <el-form-item>
          <span style="color: red;">单次只能选择单次插入或者批量插入</span>
        </el-form-item>
        <el-form-item label="批量选择课时">
          <el-col :span="11">
      <el-select v-model="starsection" placeholder="开始第几节课"  :disabled="form.section!=null">
            <el-option
              v-for="item in 16"
              :key="item.id"
              :label="item"
              :value="item"
            ></el-option>
        </el-select>
    </el-col>
   
    <el-col class="line" :span="2" style="text-align: center;">-</el-col>
    <el-col :span="11">
      <el-select v-model="endsection" placeholder="结束第几节课"  :disabled="form.section!=null">
            <el-option
              v-for="item in 16"
              :key="item.id"
              :label="item"
              :value="item"
            ></el-option>
        </el-select>
    </el-col>
  </el-form-item>




        <el-form-item label="教室名称" prop="courseName">
          <el-select v-model="roomList" placeholder="请选择教室">
            <el-option
              v-for="item in roomLists"
              :key="item.id"
              :label="item.roomName"
              :value="item.roomId"
            ></el-option>
          </el-select>
        </el-form-item>


        

        <el-form-item label="班级名称" prop="className">
          <el-select v-model="classNameList" placeholder="请输入班级名称">
            <el-option
              v-for="item in classList"
              :key="item.id"
              :label="item.name"
              :value="item.classId"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="课程和老师" prop="teacherName" label-width="120">
          <el-select v-model="selectedTeacher" placeholder="请输入课程和老师">
            <el-option
              v-for="item in tccLists"
              :key="item.id"
              :label="item.name"
              :value="item.courseId"
            ></el-option>
          </el-select>
        </el-form-item>

        
<!-- 
        <el-form-item label="教师名称" prop="teacherName">
          <el-select
            v-model="selectedTeacher"
            filterable
            placeholder="请选择"
            remote
            :loading="counsellorLoading"
            :remote-method="teacherListRemoteMethod"
          >
            <el-option
              v-for="item in teacherList"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="课程名称" prop="className">
          <el-input v-model="form.className" placeholder="请输入课程名称" />
        </el-form-item> -->
        <el-form-item label="年份" prop="year">
          <el-input v-model="form.year" placeholder="请输入年份" />
        </el-form-item>
        <el-form-item label="学期" prop="semester">
          <el-input
            v-model="form.semester"
            placeholder="请输入学期(1-上半学期，2-下半学期)"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSch, getSch, delSch, addSch, updateSch } from "@/api/edu/sch";
import { listCourse } from "@/api/edu/course";
import { listClass } from "@/api/edu/class";
import { listTeacher } from "@/api/system/user";
import { listTcc } from "@/api/edu/tcc";
import { listRoomByStatus } from "@/api/edu/room";

export default {
  name: "Sch",
  data() {
    return {
      // 遮罩层
      loading: true,
      teacherList: null,
      selectedTeacher: null,
      courseNameList: null,
      classNameList: null,
      roomList: null,
      year: null, //当前年份
      semester: null, //当前学期
      // 选中数组
      ids: [],
      starweek:null,
      endweek:null,
      starsection:null,
      endsection:null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课表格数据
      schList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      roomLists: [],
      tccList: [],
      tccLists:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tccId: null,
        week: null,
        section: null,
        day: null,
        courseId: null,
        courseName: null,
        teacherId: null,
        teacherName: null,
        classId: null,
        className: null,
        year: null,
        semester: null,
        roomName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        semester: [
          {
            required: true,
            message: "学期(1-上半学期，2-下半学期)不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getCourse(), this.getClassList();
    this.getRoomList();
    this.getCurrentYear();
    this.queryParams.year = this.form.year;
    this.queryParams.semester = this.form.semester;
    this.getListTcc();
  },
  methods: {
    /** 查询课列表 */
    getList() {
      this.loading = true;
      listSch(this.queryParams).then((response) => {
        this.schList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getListTcc() {
      this.loading = true;
      listTcc(this.queryParams).then((response) => {
        this.tccList = response.rows;
        this.loading = false;
      });
   
    },
    //获取当前年份
    getCurrentYear() {
      this.getConfigKey("edu.current.year.semester").then((response) => {
        let yearSemester = response.msg;
        let yearSemesterArr = yearSemester.split("-");
        this.year = Number(yearSemesterArr[0]);
        this.semester = Number(yearSemesterArr[1]);
      });
    },

    //远程获取教师
    teacherListRemoteMethod(query) {
      if (!this.teacherList) {
        this.counsellorLoading = true;
        listTeacher(query)
          .then((res) => {
            console.log(res);
            this.teacherList = res.data;
            this.counsellorLoading = false;
          })
          .catch((err) => {
            console.log(err);
            this.$modal.msgError(err.message);
          });
      }
    },
    getCourse() {
      listCourse(this.queryParams).then((response) => {
        this.courseName = response.rows;
      });
    },
    getClassList() {
      listClass(this.queryParams).then((response) => {
        this.classList = response.rows;
      });
    },
    getRoomList() {
      this.loading = true;
      listRoomByStatus().then((response) => {
        this.roomLists = response.rows;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        couSchId: null,
        tccId: null,
        week: null,
        day: null,
        section: null,
        courseId: null,
        courseName: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        teacherId: null,
        teacherName: null,
        classId: null,
        className: null,
        year: null,
        semester: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      
      
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.couSchId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课";
      this.teacherListRemoteMethod("");
      (this.courseNameList = ""), (this.classNameList = "");
      this.selectedTeacher = "";
      this.roomList = "";
      this.form.year = this.year;
      this.form.semester = this.semester;
      this.tccList.forEach((item) => {
        var obj ={
         name:item.courseName + "-" + item.teacherName,
         courseId:item.courseId,
         teacherId:item.teacherId
        }
        this.tccLists.push(obj)
      });
      this.starweek = null
      this.endweek = null
      this.form.week = null
      this.starsection = null
      this.endsection = null

     
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const couSchId = row.couSchId;
      getSch(couSchId).then((response) => {
        this.form = response.data;
        (this.selectedTeacher = response.data.teacherId),
          (this.courseNameList = response.data.courseId),
          (this.classNameList = response.data.classId),
          (this.studentNames = response.data.studentId);
        this.open = true;

        this.title = "修改课";
        this.teacherListRemoteMethod("");
        this.starweek = null,
      this.endweek = null
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.couSchId != null) {
            updateSch(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if(this.form.week == null ){
              this.form.weeks = this.starweek+"-"+this.endweek
            }
            if(this.form.section == null ){
              this.form.sections = this.starsection+"-"+this.endsection
            }
            addSch(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const couSchIds = row.couSchId || this.ids;
      this.$modal
        .confirm('是否确认删除课编号为"' + couSchIds + '"的数据项？')
        .then(function () {
          return delSch(couSchIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "edu/sch/export",
        {
          ...this.queryParams,
        },
        `sch_${new Date().getTime()}.xlsx`
      );
    },
  },
  watch: {
    selectedTeacher(value) {
      console.log(this.tccLists);
      // 根据选中的教师ID值获取对应的教师名称
      const selectedTeacher = this.tccLists.find(
        (item) => item.courseId === value
      );
      if (selectedTeacher) {
        console.log(selectedTeacher);
         const nickNameandteacher = selectedTeacher.name.split("-");
        // 将教师名称和ID值存储到表单中
        this.form.courseName = nickNameandteacher[0];
        this.form.teacherName = nickNameandteacher[1];
        this.form.teacherId = selectedTeacher.teacherId;
        this.form.courseId = selectedTeacher.courseId;

      }
    },
    courseNameList(value) {
      // 根据选中的课程ID值获取对应的教师名称
      const courseNameList = this.courseName.find(
        (item) => item.courseId === value
      );
      if (courseNameList) {
        this.form.courseId = courseNameList.courseId;
        this.form.courseName = courseNameList.name;
      }
    },
    classNameList(value) {
      // 根据选中的班级ID值获取对应的教师名称
      console.log(this.classList);
      const classNameList = this.classList.find(
        (item) => item.classId === value
      );
      if (classNameList) {
        // classList
        this.form.classId = classNameList.classId;
        this.form.className = classNameList.name;
      }
      console.log(this.form.classId);
      this.StudentRemoteMethod(this.form.classId);
    },

    roomList(value) {
      // 根据选中的班级ID值获取对应的教师名称

      const roomList = this.roomLists.find((item) => item.roomId === value);
      if (roomList) {
        // classList
        this.form.roomId = roomList.roomId;
        this.form.roomName = roomList.roomName;
      }
    },

    studentNames(value) {
      // 根据选中的班级ID值获取对应的教师名称
      const studentNames = this.studentList.find(
        (item) => item.studentId === value
      );
      if (studentNames) {
        console.log(studentNames);
        // classList
        this.form.studentId = studentNames.studentId;
        this.form.studentName = studentNames.nickName;
      }
    },
  },
};
</script>
